package com.ruoyi.alarm.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.alarm.mapper.AjtAlarmDataMapper;
import com.ruoyi.alarm.domain.AjtAlarmData;
import com.ruoyi.alarm.service.IAjtAlarmDataService;

/**
 * 预警信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-30
 */
@Service
public class AjtAlarmDataServiceImpl implements IAjtAlarmDataService 
{
    @Autowired
    private AjtAlarmDataMapper ajtAlarmDataMapper;

    /**
     * 查询预警信息
     * 
     * @param id 预警信息主键
     * @return 预警信息
     */
    @Override
    public AjtAlarmData selectAjtAlarmDataById(Long id)
    {
        return ajtAlarmDataMapper.selectAjtAlarmDataById(id);
    }

    /**
     * 查询预警信息列表
     * 
     * @param ajtAlarmData 预警信息
     * @return 预警信息
     */
    @Override
    public List<AjtAlarmData> selectAjtAlarmDataList(AjtAlarmData ajtAlarmData)
    {
        return ajtAlarmDataMapper.selectAjtAlarmDataList(ajtAlarmData);
    }

    /**
     * 新增预警信息
     * 
     * @param ajtAlarmData 预警信息
     * @return 结果
     */
    @Override
    public int insertAjtAlarmData(AjtAlarmData ajtAlarmData)
    {
        return ajtAlarmDataMapper.insertAjtAlarmData(ajtAlarmData);
    }

    /**
     * 修改预警信息
     * 
     * @param ajtAlarmData 预警信息
     * @return 结果
     */
    @Override
    public int updateAjtAlarmData(AjtAlarmData ajtAlarmData)
    {
        return ajtAlarmDataMapper.updateAjtAlarmData(ajtAlarmData);
    }

    /**
     * 批量删除预警信息
     * 
     * @param ids 需要删除的预警信息主键
     * @return 结果
     */
    @Override
    public int deleteAjtAlarmDataByIds(Long[] ids)
    {
        return ajtAlarmDataMapper.deleteAjtAlarmDataByIds(ids);
    }

    /**
     * 删除预警信息信息
     * 
     * @param id 预警信息主键
     * @return 结果
     */
    @Override
    public int deleteAjtAlarmDataById(Long id)
    {
        return ajtAlarmDataMapper.deleteAjtAlarmDataById(id);
    }

    @Override
    public List<Map<String, Object>> statisticsMileage(int limit) {
        return ajtAlarmDataMapper.statisticsMileage(limit);
    }

    @Override
    public List<Map<String, Object>> getTop10AlarmVehiclesInLastDays(Date startTime) {
        List<AjtAlarmData> alarmDataList = ajtAlarmDataMapper.getTop10AlarmVehiclesInLastDays(startTime);
        return processAlarmData(alarmDataList);
    }

    private List<Map<String, Object>> processAlarmData(List<AjtAlarmData> alarmDataList) {
        Map<Long, List<AjtAlarmData>> vehicleDataMap = new HashMap<>();
        // 按照车辆ID分组
        for (AjtAlarmData data : alarmDataList) {
            vehicleDataMap.putIfAbsent(data.getVehicleId(), new ArrayList<>());
            vehicleDataMap.get(data.getVehicleId()).add(data);
        }

        List<Map<String, Object>> resultList = new ArrayList<>();
        for (Map.Entry<Long, List<AjtAlarmData>> entry : vehicleDataMap.entrySet()) {
            Long vehicleId = entry.getKey();
            List<AjtAlarmData> vehicleAlarmDataList = entry.getValue();

            JSONObject item = new JSONObject();
            item.put("sim", vehicleAlarmDataList.get(0).getSim());
            item.put("vehicleId", vehicleId);
            item.put("vehicleType", getVehicleTypeDesc(vehicleAlarmDataList.get(0).getVehicleType()));
            item.put("vehicleNo", vehicleAlarmDataList.get(0).getVehicleNo());
            item.put("alarmCount", vehicleAlarmDataList.size());

            JSONArray detailArray = new JSONArray();
            for (AjtAlarmData data : vehicleAlarmDataList) {
                JSONObject detailItem = new JSONObject();
                detailItem.put("alarmId", data.getAlarmId());
                detailItem.put("startTime", data.getStartTime());
                detailItem.put("warnType", getWarnTypeDesc(data.getWarnType()));
                detailArray.add(detailItem);
            }
            item.put("detail", detailArray);

            resultList.add(item);
        }

        return resultList;
    }

    private String getWarnTypeDesc(Long warnType) {
        if (warnType == 1) {
            return "疲劳驾驶报警";
        } else if (warnType == 2) {
            return "接打电话报警";
        } else if (warnType == 3) {
            return "抽烟报警";
        } else if (warnType == 4) {
            return "分神驾驶报警(不目视前方报警(粤标))";
        } else if (warnType == 5) {
            return "驾驶员异常报警";
        } else if (warnType == 6) {
            return "探头遮挡报警(粤标)";
        } else if (warnType == 7) {
            return "疲劳（打哈欠）报警";
        } else if (warnType == 8) {
            return "终端主电源欠压";
        } else if (warnType == 9) {
            return "终端主电源掉电";
        } else if (warnType == 10) {
            return "未系安全带报警(粤标)";
        }
        return "未知报警类型";
    }

    private String getVehicleTypeDesc(Long vehicleType) {
        if (vehicleType == 0) {
            return "自卸车";
        } else if (vehicleType == 1) {
            return "挖掘机";
        }
        return "未知车辆类型";
    }
}
